package com.project.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.SupplierBean;
import com.project.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("supplier")
public class SupplierController {
    @Autowired
    private ISupplierService supplierService;
    @RequestMapping("findByItem")
    public IPage<SupplierBean> findByItem(Integer pageNO, String name, String supRange) {
        return supplierService.findByItem(pageNO, name, supRange);
    }
    @RequestMapping("findById")
    public SupplierBean findById(Integer id){
        return supplierService.findById(id);
    }
    @RequestMapping("update")
    public String update(Integer id, String name, String phone){
        supplierService.update(id, name, phone);
        return "ok";
    }
    @RequestMapping("add")
    public String add(SupplierBean supplier){
        supplierService.add(supplier);
        return "ok";
    }
    @RequestMapping("findAll")
    public List<SupplierBean> findAll() {
        return supplierService.findAll();
    }
    @RequestMapping("addList")
    public String addList(@RequestParam("excelFile") MultipartFile mf) throws Exception {
        //定义导入设置
        ImportParams ims = new ImportParams();
        //设置标题占几行
        ims.setTitleRows(1);
        //设置表头
        ims.setHeadRows(1);
        //设置读取第几个选项卡内容
        ims.setSheetNum(1);

        //将上传excel文件流，转化为集合，第一个参数为上传文件读取流，第二个参数为元素集合的类模板
        //第三参数导入设置对象
        List<SupplierBean> list = ExcelImportUtil.importExcel(mf.getInputStream(), SupplierBean.class, ims);
        supplierService.addList(list);
        if (list==null){
            return "no";
        }else {
            return "ok";
        }
    }
}
